import { _decorator, Component, instantiate, Node, Prefab, random, UITransform } from 'cc';
import { pierController } from './pierController';
const { ccclass, property } = _decorator;

@ccclass('piersRootController')
export class piersRootController extends Component {
    @property(Prefab) pire:Prefab
    start() 
    {
        this.init();
    }
    init()
    {
        //st子节点数组
        let st = this.node.children
        //将所有子节点IsStart设为true
        for(let i = 0;i<st.length;i++)
        {
            st[i].getComponent(pierController).IsStart = true;
        }
    }
    //创建新的桥墩
    create()
    {
        const chi = this.node.children
        //获取最后一个桥墩宽度
        const pireWidth = chi[chi.length - 1].getComponent(UITransform).contentSize.width;
        const gap = Math.random() * (300 - pireWidth + 100) + pireWidth + 100
        const pire = instantiate(this.pire);
        this.node.addChild(pire);
        pire.setPosition(gap + chi[chi.length - 2].getPosition().x,-360)

    }
    update(deltaTime: number) {
        
    }
}


